/**
 * Created by Diego on 16/3/22.
 * type=1:题目评论,type=2:学习资料评论,type=3:讨论区评论,type=4:评论的评论
 *
 */

 //date format
 Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
$(function(){
    //取消 1 2 3
    $(".qa-a-reply .qa-ql_command_cancel").click(function(){
        $(this).parent().addClass("none");
    });
//    取消4
    $(".ql_command_cancel").click(function(){
        $(this).parent().parent(".ql_comment_form").toggle();
    });
//聚焦回复
    $(".qa-a-reply input").focus(function(){
        $(this).next().removeClass("none");
    });
	$("div.ql_comment_editable").focus(function(){
 	  $(this).removeClass("ql_comment_empty").next().removeClass("none");
	}).blur(function(){
  		 if($(this).html()==""){
      		 $(this).addClass("ql_comment_empty");
  		 }
	})
	$(".qa-a-reply .ql_comment_editable").focus(function(){
		$(this).removeClass("ql_comment_empty").next().removeClass("none");	
	}).blur(function(){
		if($(this).html()==""){
			$(this).addClass("ql_comment_empty");
		}
	})
//展开评论
    $(".qa-a-item-meta a._pl").each(function(){
        var _this=this;
        var oldHtml=$(_this).html();
        $(_this).click(function(){
            $(this).parent().parent().siblings(".dc").toggleClass("none");
            if(!$(this).parent().parent().siblings(".dc").hasClass("none"))
            {
                $(this).html('<i style="padding-left: 5px; font-size: 12px;" class="iconfont icon-huifu-copy"></i> 收起评论');//加了个ico
            }
            else{
                $(this).html(oldHtml);
            }
        });
    });
//点击回复
    $(".dc .reply").each(function(){
        var _this=this;
        $(_this).click(function(){
            $(this).parent().parent().parent().siblings().find(".ql_comment_form").hide();
            $(this).parent().siblings(".ql_comment_form").toggle();
        })
    });
//评论 1 2 3

    function reply(){
        var token=$(this).attr("data-token"),
            type=$(this).attr("data-rtype"),
            content=$(this).parent().prev().html().replace(/<[^>].*?>/g,""),
            html="",
            id=$(this).attr("data-id"),//题目id
            mid=$(this).attr("data-mid"),
            img=$(this).attr("data-img"),
            name=$(this).attr("data-name");
        var answer = $(this).closest('.answer');
        var that=this;
        if(content!="")
        {
            $.post("/api/comment",{id:id,content:content,_token:token,type:type},function(data){
                html='<div class="media">'+
                    '<a class="pull-left" target="_blank" href="#">'+
                    '<img class="img-circle" alt="Cinque Terre" width="20" height="20" src="'+img+'"> </a>'+
                    '<div class="media-body">'+
                    '<h4 class="media-heading"><a class="gray" href="/profile/'+mid+'">'+name+'</a></h4><div class="comment-content">'+ content+
                    '</div><div class="ql_comment_ft"> <span class="date">'+new Date().Format("yyyy-MM-dd hh:mm:ss"); +' </span>'+
                '<a href="javascript:;" class="reply ql_comment_op_link gray none" name="reply_comment">回复</a> </div>'+
                '<div class="ql_comment_form expanded" style="display: none;">'+
                '<div class="ql_comment_editable_wrap">'+
                '<input class="ql_comment_editable editable" placeholder="写下你的回复" type="text"> </div>'+
                '<div class="ql_command ql_clear">'+
                '<a href="javascript:;" class="btn btn-primary pull-right replyBtn" name="">回复</a>'+
                '<a href="javascript:;" class="ql_comment_close ql_command_cancel">取消</a> </div>'+
                '<div class="ql_comment-info"> </div> </div> </div> </div>';
                $("#"+id).find(".qa-comment-list").append(html);
                if (content.length >= 20) {
                    answer.find('button.down').attr('commented', 1);
                }
                //clear hide
                $(that).parent().prev().html("").removeClass("ql_comment_empty");
                $(that).parent().addClass("none");
                $('html,body').animate({scrollTop:$("#"+id).find(".qa-a-reply").offset().top-100}, 800);
            });

        }
        //else{
        //    $(this).parent().prev().css({"border-color":"red"}).attr("placeholder","内容不能为空哦^_^:");
        //    return null;
        //}
    }

    $(".qa-replyBtn").click(reply);
    //评论4
    $(".replyBtn").click(function(){
        var token=$(this).attr("data-token"),
            content=$(this).parent().prev().find("input").val(),
            html="",
            rid=$(this).attr("data-rid"),
            mid=$(this).attr("data-mid"),
            id=$(this).attr("data-id"),
            img=$(this).attr("data-img"),
            rname=$(this).parent().parent().siblings(".replyName").val(),
            name=$(this).attr("data-name"),
            type=$(this).attr("data-rtype");
        var that=this;
        if(content!=""){
            $.post("/api/comment",{id:id,ruserid:rid,content:content,_token:token,type:type},function(data){
                console.log(data);
                html='<div class="media">'+
                    '<a class="pull-left" target="_blank" href="#">'+
                    '<img class="img-circle" alt="Cinque Terre" width="20" height="20" src="'+img+'"> </a>'+
                    '<div class="media-body"> <div style="display:none">'+name+'</div>'+
                    '<h4 class="media-heading">'+
                    '<a href="/profile/'+mid+'" class="username gray" target="_blank">'+name+'</a>'+
                    '<small> 回复 </small><a href="/profile/'+rid+'" class="username gray" target="_blank">'+rname+'</a>'+
                    '</h4><div class="comment-content">'+content+'</div>'+
                    '<div class="ql_comment_ft">'+
                    '<span class="date">'+new Date().Format("yyyy-MM-dd hh:mm:ss");+'</span>'+
                    '<a href="javascript:;" class="reply ql_comment_op_link gray none" name="reply_comment">回复</a>'+
                    '</div>'+
                    '<div class="ql_comment_form expanded" style="display: none;">'+
                    '<div class="ql_comment_editable_wrap">'+
                    '<input class="ql_comment_editable editable" placeholder="写下你的回复" type="text">'+
                    '</div> <div class="ql_command ql_clear">'+
                    '<a href="javascript:;" class="btn btn-primary pull-right replyBtn" name="">回复</a>'+
                    '<a href="javascript:;" class="ql_comment_close ql_command_cancel">取消</a> </div> </div> </div></div>';
                $("#"+id).find(".qa-comment-list").append(html);
                $(that).parent().parent().toggle();
                $(that).parent().prev().find("input").val("");
                $('html,body').animate({scrollTop:$("#"+id).find(".qa-a-reply").offset().top-100}, 800);
            });
            
        }
    })
    
    $(".deleteReply").click(function () {
        var dataid = $(this).attr('data-id');
        var that = this;
        $.get('/deldis/' + dataid, function () {
            var reply = $(that).closest('.media');
            reply.hide();
        });
    })
});
